package com.huicoo.forestmanager.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.network.bean.patrol.PatrolEventEntity;
import com.huicoo.forestmanager.util.glide.ExploreImageLoader;
import com.liaoinstan.springview.utils.DensityUtil;

import java.util.List;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/8		    Administrator
 * 巡查任务--需进一步核实问题的适配器
 */
public class PatrolNeedVerifyProblemAdapter extends BaseQuickAdapter<PatrolEventEntity, BaseViewHolder> {
    public static final int ATTACHMENT_VOICE = 1;
    public static final int ATTACHMENT_IMAGE = 2;
    public static final int ATTACHMENT_VIDEO = 3;

    private OnAttachmentListener onAttachmentListener;

    public PatrolNeedVerifyProblemAdapter(@Nullable List<PatrolEventEntity> data) {
        super(R.layout.adapter_patrol_need_verify_problem_item, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, PatrolEventEntity item) {
        // 事件类型
        holder.setText(R.id.tv_type, item.getEventType() + "  " + item.getEventChildType());
        // 状态 0未受理 1正在处理 100已处理
        String status = "";
        switch (item.getStatus()) {
            case "0":
                status = "未受理";
                break;
            case "1":
                status = "正在处理";
                break;
            case "100":
                status = "已处理";
                break;
            default:
                break;
        }
        holder.setText(R.id.tv_status, status);
        // 事件描述
        holder.setText(R.id.tv_desc, TextUtils.isEmpty(item.getDescription()) ? "" : item.getDescription());
        //事件编号
        holder.setText(R.id.tv_no, "事件编号：" + (TextUtils.isEmpty(item.getEventNum()) ? "" : item.getEventNum()));
        //事件地点和时间
        holder.setText(R.id.tv_info, (TextUtils.isEmpty(item.getAddress()) ? "" : item.getAddress() + " ") + (TextUtils.isEmpty(item.getReportingTime()) ? "" : item.getReportingTime()));
        //图片
        ImageView iv_pic = holder.itemView.findViewById(R.id.iv_pic);
        String picUrl = "";
        //显示图片附件的第一张图片
        if (!TextUtils.isEmpty(item.getImagePath())) {
            String[] picPaths = item.getImagePath().split("\\|");
            for (String path : picPaths) {
                if (!TextUtils.isEmpty(path)) {
                    picUrl = path;
                    break;
                }
            }
        }
        //没有图片附件，显示视频附件的第一个缩略图
        if (TextUtils.isEmpty(picUrl)) {
            if (!TextUtils.isEmpty(item.getThumbnailPath())) {
                String[] thumbnailPaths = item.getThumbnailPath().split("\\|");
                for (String path : thumbnailPaths) {
                    if (!TextUtils.isEmpty(path)) {
                        picUrl = path;
                        break;
                    }
                }
            }
        }
        ExploreImageLoader.getInstance().loadImage(mContext, picUrl, iv_pic);
        String finalPicUrl = picUrl;
        iv_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAttachmentListener != null) {
                    onAttachmentListener.clickPicAttachment(finalPicUrl);
                }
            }
        });
    }

    private ImageView createAttachmentItem(int attachmentType) {
        ImageView imageView = new ImageView(mContext);
        LinearLayout.LayoutParams layoutParams;
        // 图片附件宽度90，其他附件宽度60
        int dp90 = DensityUtil.dp2px(90);
        int dp60 = DensityUtil.dp2px(60);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (attachmentType == ATTACHMENT_IMAGE) {
            layoutParams = new LinearLayout.LayoutParams(dp90, dp60);
        } else {
            layoutParams = new LinearLayout.LayoutParams(dp60, dp60);
        }
        layoutParams.rightMargin = DensityUtil.dp2px(10);
        imageView.setLayoutParams(layoutParams);
        return imageView;
    }

    public interface OnAttachmentListener {
        void clickMediaAttachment(int type, String path, String filename);

        void clickPicAttachment(String path);

    }

    public void setOnAttachmentListener(OnAttachmentListener onAttachmentListener) {
        this.onAttachmentListener = onAttachmentListener;
    }
}
